﻿using CSRedis;
using DotNetCore.CAP;
using DotNetCore.CAP.Messages;
using EventBusETO.CustomerType;
using EventBusETO.RoomType;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ActionConstraints;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.OpenApi.Models;
using MiniExcelLibs;
using Newtonsoft.Json;
using SH.Common.CommonReturn;
using SH.Order.Application.IServices.Dto;
using SH.Order.Application.IServices.IOrder;
using SH.Order.Application.Service;
using SH.Order.Domain.OrderClass;
using SH.Order.ErrorCodeEnum;
using SqlSugar;
using System.Data;
using System.Linq.Expressions;

namespace SH.Order.Read.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class OrderInfoController : ControllerBase
    {
        private readonly IOrderServices _orderservices;
        private readonly IHttpClientFactory _httpclientFactory;
        private readonly CSRedisClient _redis;
        public OrderInfoController(IOrderServices orderservices, IHttpClientFactory httpClientFactory)
        {
            _orderservices = orderservices;
            _httpclientFactory = httpClientFactory;
            _redis = new CSRedisClient(AppService.Configuration["redisIP"]);
        }
        /// <summary>
        /// 订房订单显示
        /// </summary>
        /// <param name="StartTime">开始时间</param>
        /// <param name="EndTime">结束时间</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="Name">订单号</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult OrderInfoShow(DateTime? StartTime, DateTime? EndTime, int pageSize, int pageIndex, string? Name = "", int RTid = -1, int paymenttypesId = -1, int ordertypes = -1, int orderStatus = -1)
        {
            return Ok(_orderservices.OrderInfoShow(StartTime, EndTime, pageSize, pageIndex, Name, RTid, paymenttypesId, ordertypes, orderStatus));
        }
        /// <summary>
        /// 导出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> DownloadExcel()
        {
            var Orders = await _orderservices.GetListAsync();
            List<object> list = new List<object>();
            foreach (var item in Orders)
            {
                list.Add(new
                {
                    订单编号 = item.OrderNo,
                    入住时间 = item.EntryTime,
                    离开时间 = item.LeaveTime,
                    实付金额 = item.ActualPay,
                }
                 );
            }
            var memoryStream = new MemoryStream();
            memoryStream.SaveAs(list);
            memoryStream.Seek(0, SeekOrigin.Begin);
            return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = "订房订单.xlsx"
            };
        }
        /// <summary>
        /// 跨api访问接口(房型)
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult AddHttpClient()
        {
            var client = _httpclientFactory.CreateClient();
            var response = client.GetAsync("http://1.92.78.23:9003/api/RoomType/GetRoomList").Result;
            if (response.IsSuccessStatusCode)
            {
                var message = response.Content.ReadAsStringAsync().Result;
                var customerlist = JsonConvert.DeserializeObject<List<RoomTypeEto>>(message);
                return Ok(customerlist);
            }
            return Ok("");
        }
        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Delete1(int id)
        {
            ApiResult<int> apiResult = new ApiResult<int>();
            var order = _orderservices.Delete1(id);
            apiResult.Success();
            return Ok(apiResult);
        }
        [HttpGet]
        public IActionResult OrderInfoApiShow()
        {
          return Ok(_orderservices.OrderInfoApiShow());
        }
        /// <summary>
        /// 获取所有的订单（租户模块HttpClient访问）
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetAllOrders()
        {
            string tenantId = _redis.Get<string>("tenantId");
            var result = await _orderservices.All.Where(x => x.TenantId == tenantId).ToListAsync();
            //ApiResult<List<OrderInfo>> apiResult = new ApiResult<List<OrderInfo>>();
            //apiResult.Success(result);
            return Ok(result);
        }
    }
}
